home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  2.4 KB  |  96 lines

  1. #include "lib.h"
  2. #include <string.h>
  3. /*
  4. **    @(#)getopt.c    2.2 (smail) 1/26/87
  5. */
  6.  
  7. /*
  8.  * Here's something you've all been waiting for:  the AT&T public domain
  9.  * source for getopt(3).  It is the code which was given out at the 1985
  10.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  11.  * directly from AT&T.  The people there assure me that it is indeed
  12.  * in the public domain.
  13.  * 
  14.  * There is no manual page.  That is because the one they gave out at
  15.  * UNIFORUM was slightly different from the current System V Release 2
  16.  * manual page.  The difference apparently involved a note about the
  17.  * famous rules 5 and 6, recommending using white space between an option
  18.  * and its first argument, and not grouping options that have arguments.
  19.  * Getopt itself is currently lenient about both of these things White
  20.  * space is allowed, but not mandatory, and the last option in a group can
  21.  * have an argument.  That particular version of the man page evidently
  22.  * has no official existence, and my source at AT&T did not send a copy.
  23.  * The current SVR2 man page reflects the actual behavor of this getopt.
  24.  * However, I am not about to post a copy of anything licensed by AT&T.
  25.  */
  26.  
  27.  
  28. /*LINTLIBRARY*/
  29. #ifndef NULL
  30. #define NULL    0
  31. #endif
  32.  
  33. #define EOF    (-1)
  34. #define ERR(s, c)    if(opterr){\
  35.     char errbuf[2];\
  36.     errbuf[0] = c; errbuf[1] = '\n';\
  37.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  38.     (void) write(2, s, (unsigned)strlen(s));\
  39.     (void) write(2, errbuf, 2);}
  40.  
  41. #ifndef __STDC__    /* for __STDC__ pick up proto from <std.h> */
  42. extern char *index();
  43. extern int write();
  44. #endif
  45.  
  46. int    opterr = 1;
  47. int    optind = 1;
  48. int    optopt;
  49. char    *optarg;
  50.  
  51. int
  52. getopt(argc, argv, opts)
  53. int    argc;
  54. char    **argv, *opts;
  55. {
  56.     static int sp = 1;
  57.     register int c;
  58.     register char *cp;
  59.  
  60.     if(sp == 1)
  61.         if(optind >= argc ||
  62.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  63.             return(EOF);
  64.         else if(strcmp(argv[optind], "--") == 0) {
  65.             optind++;
  66.             return(EOF);
  67.         }
  68.     optopt = c = argv[optind][sp];
  69.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  70.         ERR(": illegal option -- ", c);
  71.         if(argv[optind][++sp] == '\0') {
  72.             optind++;
  73.             sp = 1;
  74.         }
  75.         return('?');
  76.     }
  77.     if(*++cp == ':') {
  78.         if(argv[optind][sp+1] != '\0')
  79.             optarg = &argv[optind++][sp+1];
  80.         else if(++optind >= argc) {
  81.             ERR(": option requires an argument -- ", c);
  82.             sp = 1;
  83.             return('?');
  84.         } else
  85.             optarg = argv[optind++];
  86.         sp = 1;
  87.     } else {
  88.         if(argv[optind][++sp] == '\0') {
  89.             sp = 1;
  90.             optind++;
  91.         }
  92.         optarg = NULL;
  93.     }
  94.     return(c);
  95. }
  96.